<?php
// +----------------------------------------------------------------------
// | 卡券
// +----------------------------------------------------------------------
// | Copyright (c) 2015-2024 http://www.yicmf.com, All rights reserved.
// +----------------------------------------------------------------------
// | Author: 微尘 <yicmf@qq.com>
// +----------------------------------------------------------------------
namespace app\controller\api\v3;


use app\model\app\AppTaskOrder as AppTaskOrderModel;
use app\model\app\AppTaskPz as AppTaskPzModel;
use app\model\app\Modular as ModularModel;
use app\model\app\AppTask as AppTaskModel;
use app\model\app\AppTaskBb as AppTaskBbModel;
use app\event\app\Tui as TuiEvent;

class Tui extends Common
{

    /**
     * APP推广主页
     */
    public function home()
    {
        $data['tasks'] = AppTaskModel::where('tui_status', 1)->where('is_online', 1)->where('status', 1)->paginate();
        $data['modular'] = ModularModel::with('cover')->where('id', 3)->find();
        $data['sheets'][] = ['id'=>1,'name'=>'热门','is_hot'=>1];
        $data['sheets'][] = ['id'=>2,'name'=>'新上','is_hot'=>1];
        $data['sheets'][] = ['id'=>3,'name'=>'地推','is_hot'=>0];
        $data['banners'][] = ['id'=>3,'image'=>'','is_hot'=>0];
        $data['sheet_id'] = $this->request->get('sheet_id',1);

        return $this->successAjax($data);
    }
    /**
     * 搜索
     */
    public function search()
    {
        $keywords = $this->request->get('keywords', '');
        $data['tasks'] = AppTaskModel::where('tui_status', 1)->where('status', 1)->where('is_online', 1)->when($keywords, function ($query) use ($keywords) {
            $query->where('name', 'like', '%' . $keywords . '%');
        })->paginate();
        $data['modular'] = ModularModel::with('cover')->where('id', 3)->find();
        $data['sheets'] = [];
        return $this->successAjax($data);
    }

    /**
     * 申请
     */
    public function apply()
    {
        if ($this->request->isPost()) {
            $event = new TuiEvent();
            $data = $event->apply($this->request->param(), $this->user);
            return $this->successAjax($data);
        } else {

            return $this->errorAjax('请求错误');
        }
    }

    /**
     * 我的报备
     */
    public function bb()
    {
        $data['lists'] = AppTaskBbModel::where('user_id', $this->user['id'])->where('app_task_id',$this->request->param('app_task_id'))->paginate();
        $pass = AppTaskBbModel::where('user_id', $this->user['id'])->where('status', 1)->where('app_task_id',$this->request->param('app_task_id'))->count();
        $no = AppTaskBbModel::where('user_id', $this->user['id'])->where('status', 2)->where('app_task_id',$this->request->param('app_task_id'))->count();
        $wait = AppTaskBbModel::where('user_id', $this->user['id'])->where('status', 3)->where('app_task_id',$this->request->param('app_task_id'))->count();
        $data['sheets'][] = [
            'name' => '已通过',
            'num' => $pass,
        ];
        $data['sheets'][] = [
            'name' => '未通过',
            'num' => $no,
        ];
        $data['sheets'][] = [
            'name' => '审核中',
            'num' => $wait,
        ];
        return $this->successAjax($data);
    }

    /**
     * 我的凭证
     */
    public function pz()
    {
        if ($this->request->isPost())
        {
            $tui = new TuiEvent();
            $res = $tui->subPz($this->request->param(), $this->user);
            return $this->successAjax($res);
        }else{
            $data['lists'] = AppTaskPzModel::where('user_id', $this->user['id'])->paginate();
            return $this->successAjax($data);
        }

    }

    /**
     * 订单记录
     */
    public function orders()
    {
        $data['lists'] = AppTaskOrderModel::where('user_id', $this->user['id'])->where('app_task_bb_id', $this->request->param('app_task_bb_id'))->paginate();

        return $this->successAjax($data);
    }

    /**
     * 推广详情
     */
    public function info()
    {
        $detail = AppTaskModel::where('id', $this->request->param('id'))->find();
        if ($detail) {
            $data['detail'] = AppTaskModel::where('id', $this->request->param('id'))->find();
            $data['banners'][] = ['id'=>3,'image'=>'','is_hot'=>0];
            $data['comments'] = AppTaskModel::where('tui_status', 1)->where('is_online',1)->limit(5)->select();
            $data['news'] = AppTaskModel::where('tui_status', 1)->where('is_online',1)->limit(5)->select();
            $data['my']['code'] = AppTaskBbModel::where('user_id', $this->user['id'])->where('app_task_id', $detail['id'])->count();
            $data['my']['get_amount'] =  AppTaskBbModel::where('user_id', $this->user['id'])->where('app_task_id', $detail['id'])->sum('get_amount');
            return $this->successAjax($data);
        } else {
            return $this->errorAjax();
        }

    }


}
